package com.yisu.auction.api.web.controller;

import com.yisu.auction.api.core.service.IAddressService;
import com.yisu.auction.api.domain.request.BasePage;
import com.yisu.auction.api.domain.request.memberPackage.AddressRequest;
import com.yisu.auction.api.domain.request.prizePackage.ReceivingAddressRequest;
import com.yisu.auction.api.domain.response.memberPackage.AddressListResponse;
import com.yisu.auction.api.domain.response.memberPackage.AddressResponse;
import com.yisu.auction.api.domain.vo.PrizePackageVos.ReceivingAddressVo;
import com.yisu.auction.api.web.interceptor.IAuthVerifyAnnotation;
import com.yisu.auction.ewf.common.enums.LoginTypeEnum;
import com.yisu.auction.ewf.common.enums.ResponseCode;
import com.yisu.auction.ewf.common.web.JsonResult;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

/**
 * author: 赵杰
 * Business Description： 收货地址业务
 * createTime：2017/8/16
 */
@Controller
@RequestMapping(value = "/address")
public class AddressController extends BaseController {
    @Resource
    private IAddressService iAddressService;


    /**
     * author:黄萧林
     * Business Description:添加收货地址
     *
     * @param
     * @return
     */
    @RequestMapping(value = "/addReceivingAddress", method = {RequestMethod.POST}, produces = "application/json; charset=utf-8")
    @ResponseBody
    @IAuthVerifyAnnotation(Desc = "登录验证", LoginTypeEnum = LoginTypeEnum.NEED_LOGIN)
    public JsonResult addReceivingAddress(ReceivingAddressRequest receivingAddressRequest) {
        // TODO: 2017/8/22 收货地址最多增加三个
        this.userToken = receivingAddressRequest.getUserToken();
        receivingAddressRequest.setMemberId(String.valueOf(this.getToken().getMemberId()));
        if (iAddressService.addReceivingAddress(receivingAddressRequest)) {
            List<ReceivingAddressVo> receivingAddressVos = iAddressService.getMineReceivingAddressList(receivingAddressRequest);
            if (receivingAddressVos.size()>3){
                return new JsonResult(ResponseCode.FAILURE.code, "收货地址大于三个");
            }else {
                return new JsonResult(ResponseCode.SUCCESS.code, ResponseCode.SUCCESS.msg);
            }
        }
        return new JsonResult(ResponseCode.FAILURE.code, ResponseCode.FAILURE.msg);
    }

    /**
     * author:黄萧林
     * Business Description:编辑收货地址
     *
     * @param
     * @return
     */
    @RequestMapping(value = "/getReceivingAddressInfo", method = {RequestMethod.POST}, produces = "application/json; charset=utf-8")
    @ResponseBody
    @IAuthVerifyAnnotation(Desc = "登录验证", LoginTypeEnum = LoginTypeEnum.NEED_LOGIN)
    public JsonResult getReceivingAddressInfo(Long id) {
        JsonResult jsonResult = new JsonResult();
        AddressResponse addressResponse = new AddressResponse();
        ReceivingAddressVo receivingAddressVo = iAddressService.getReceivingAddressInfo(id);
        addressResponse.setReceivingAddressVo(receivingAddressVo);
        jsonResult.setData(addressResponse);
        return jsonResult;
    }

    /**
     * author:黄萧林
     * Business Description:保存修改的收货地址
     *
     * @param
     * @return
     */
    @RequestMapping(value = "/saveReceivingAddressInfo", method = {RequestMethod.POST}, produces = "application/json; charset=utf-8")
    @ResponseBody
    @IAuthVerifyAnnotation(Desc = "登录验证", LoginTypeEnum = LoginTypeEnum.NEED_LOGIN)
    public JsonResult saveReceivingAddressInfo(ReceivingAddressVo receivingAddressVo) {
        this.userToken = receivingAddressVo.getUserToken();
        receivingAddressVo.setMemberId(String.valueOf(this.getToken().getMemberId()));
        if (iAddressService.saveReceivingAddressInfo(receivingAddressVo)) {
            return new JsonResult(ResponseCode.SUCCESS.code, ResponseCode.SUCCESS.msg);
        }
        return new JsonResult(ResponseCode.FAILURE.code, ResponseCode.FAILURE.msg);
    }

    /**
     * author:黄萧林
     * Business Description:删除收货地址
     *
     * @param
     * @return
     */
    @RequestMapping(value = "/deleteReceivingAddress", method = {RequestMethod.POST}, produces = "application/json; charset=utf-8")
    @ResponseBody
    @IAuthVerifyAnnotation(Desc = "登录验证", LoginTypeEnum = LoginTypeEnum.NEED_LOGIN)
    public JsonResult deleteReceivingAddress(AddressRequest addressRequest) {
        this.userToken = addressRequest.getUserToken();
        addressRequest.setMemberId(String.valueOf(this.getToken().getMemberId()));
        if (iAddressService.deleteReceivingAddress(addressRequest)) {
            return new JsonResult(ResponseCode.SUCCESS.code, ResponseCode.SUCCESS.msg);
        }
        return new JsonResult(ResponseCode.FAILURE.code, ResponseCode.FAILURE.msg);
    }

    /**
     * author:黄萧林
     * Business Description:获取我的收货地址列表
     *
     * @param
     * @return
     */
    @RequestMapping(value = "/getMineReceivingAddressList", method = {RequestMethod.POST}, produces = "application/json; charset=utf-8")
    @ResponseBody
    @IAuthVerifyAnnotation(Desc = "登录验证", LoginTypeEnum = LoginTypeEnum.NEED_LOGIN)
    public JsonResult getMineReceivingAddressList(BasePage basePage) {
        this.userToken = basePage.getUserToken();
        basePage.setMemberId(String.valueOf(this.getToken().getMemberId()));
        JsonResult jsonResult = new JsonResult();
        AddressListResponse addressListResponse = new AddressListResponse();
        List<ReceivingAddressVo> receivingAddressVos = iAddressService.getMineReceivingAddressList(basePage);
        addressListResponse.setReceivingAddressVos(receivingAddressVos);
        jsonResult.setData(addressListResponse);
        return jsonResult;
    }

}
